package com.java01.net.resource;

import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class GetIP {

	public static void main(String[] args) {
		try {
			GetIP test = new GetIP();
			System.out.println(test.getIP());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getIP() throws Exception {
		Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
		while (netInterfaces.hasMoreElements()) {
			NetworkInterface netIf = (NetworkInterface) netInterfaces.nextElement();

			for (InterfaceAddress ifAddr : netIf.getInterfaceAddresses()) {
				InetAddress addr = ifAddr.getAddress();
				if (addr.isSiteLocalAddress() && !addr.isLoopbackAddress()) {
					return addr.getHostAddress();
				}
			}
		}
		return null;
	}
}
